package com.example.mobile2021606127cxz0323homework;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }


    public void initView() {
        EditText edtTxt_user = findViewById(R.id.edtTxt_main_user);
        EditText edtTxt_old = findViewById(R.id.edtTxt_main_old);
        EditText edtTxt_new = findViewById(R.id.edtTxt_main_new);
        EditText edtTxt_newagain = findViewById(R.id.edtTxt_main_newagain);
        Button btn_xiugai = findViewById(R.id.btn_main_xiugai);
        btn_xiugai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String user = edtTxt_user.getText().toString();
                String oldpsw = edtTxt_old.getText().toString().trim();
                String newpsw = edtTxt_new.getText().toString().trim();
                String newagain = edtTxt_newagain.getText().toString().trim();


                if (user.length() == 0 | oldpsw.length() == 0 | newpsw.length() == 0 | newagain.length() == 0) {
                    Toast.makeText(MainActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                } else if (user.length() < 6) {
                    Toast.makeText(MainActivity.this, "用户名长度应在6~10个字之间", Toast.LENGTH_SHORT).show();
                } else if (user.length() >= 10) {
                    Toast.makeText(MainActivity.this, "用户名长度应在6~10个字之间", Toast.LENGTH_SHORT).show();
                } else if (!edtTxt_old.getText().toString().trim().equals("abc123")) {
                    Toast.makeText(MainActivity.this, "旧密码不正确，请重新输入", Toast.LENGTH_SHORT).show();
                } else if (edtTxt_new.getText().toString().trim().equals(edtTxt_newagain.getText().toString().trim())) {
                    Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "两次输入的新密码不一致，请重新检查", Toast.LENGTH_SHORT).show();
                }
                Log.i("infor", "获取到的信息为:" + "   " + "用户名：" + user + "    " + "旧密码：" + oldpsw + "    " + "新密码：" + newpsw + "    " + "确认密码：" + newagain);
            }

        });
    }


    @Override
    public void onBackPressed() {
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("退出");
        builder.setIcon(R.drawable.ic_baseline_person_24);
        builder.setMessage("是否确定退出修改：");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                MainActivity.this.finish();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog = builder.create();
        dialog.show();
    }

//    public void startActivity2(View view) {
//        startActivity(new Intent(this,MainActivity.class));
//    }
}




